package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import modelo.Administrador;
import modelo.Cliente;

import conexao.ConnectionManager;

/**
 * @author Carlos, Derek e Hellen
 */
class AdministradorDAOImp implements AdministradorDAO {


	
	private final String LIKE = "select * from administrador where login = ? and senha = ?";

	private ConnectionManager connectionManager;
	private static Connection connection;

	AdministradorDAOImp() {
		this.openConnection();
	}

	public boolean isConnectionClose() {
		try {
			return connection.isClosed();
		} catch (SQLException ex) {
			return false;
		}
	}

	public void openConnection() {
		if (connectionManager == null) {
			connectionManager = ConnectionManager.getInstance();
		}

		if (connection == null) {
			connection = connectionManager.getConnection();
		}

		if (isConnectionClose()) {
			connection = connectionManager.getConnection();
		}
	}

	public void closeConnection() {
		if (connection != null && connectionManager != null) {
			if (!this.isConnectionClose()) {
				connectionManager.closeConnection(connection);
			}
		}
	}
	
	public List<Administrador> autenticarAdministrador (Administrador administrador) {
		
		List<Administrador> administradores = new ArrayList<Administrador>();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = connection.prepareStatement(LIKE);
			// 6 informe o parametro para instrucao like
			ps.setObject(1, administrador.getLogin());
			ps.setObject(2, administrador.getSenha());

			rs = ps.executeQuery();

			while (rs.next()) {
				administradores.add(new Administrador(rs.getInt("idAdmin"),						 
						rs.getString("login"),						
						rs.getString("senha")));
			}

		} catch (SQLException ex) {
			Logger.getLogger(AdministradorDAOImp.class.getName()).log(Level.SEVERE, null,
					ex);
		} finally {
			this.closeConnection();
		}
		return administradores;
		
	}

	@Override
	public Administrador salvar(Administrador e) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void alterar(Administrador e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deletar(Administrador e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<Administrador> findAll() {
		// TODO Auto-generated method stub
		return null;
	}
	

}
